<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="员工ID" prop="employeeId">
        <el-input
          v-model="queryParams.employeeId"
          placeholder="请输入员工ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="员工姓名" prop="employeeName">
        <el-input
          v-model="queryParams.employeeName"
          placeholder="请输入员工姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="岗位" prop="jobId">
        <el-select v-model="queryParams.jobId" placeholder="岗位" clearable>
          <el-option
            v-for="item in postOptions"
            :key="item.postId"
            :label="item.postName"
            :value="item.postId"
            :disabled="item.status == 1"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="工单状态" prop="stateId">
        <el-select v-model="queryParams.stateId" placeholder="工单状态" clearable>
        <el-option
          v-for="dict in dict.type.kpi_order_state_type"
          :key="dict.value"
          :label="dict.label"
          :value="dict.value"
        />
        </el-select>
      </el-form-item>
      <el-form-item label="绩效月份" prop="performanceDate">
        <el-date-picker clearable
                        v-model="queryParams.performanceDate"
                        type="month"
                        value-format="yyyy-MM"
                        format="yyyy-MM"
                        placeholder="请选择绩效月份">
        </el-date-picker>
      </el-form-item>
<!--      <el-form-item label="工单状态" prop="stateName">
        <el-input
          v-model="queryParams.stateName"
          placeholder="请输入工单状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
<!--      <el-form-item label="总评分" prop="totalScore">-->
<!--        <el-input-->
<!--          v-model="queryParams.totalScore"-->
<!--          placeholder="请输入总评分"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="直线经理加扣分意见" prop="taskManagerScore">-->
<!--        <el-input-->
<!--          v-model="queryParams.taskManagerScore"-->
<!--          placeholder="请输入直线经理加扣分意见"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="加分项直线经理评分" prop="itemManagerScore">-->
<!--        <el-input-->
<!--          v-model="queryParams.itemManagerScore"-->
<!--          placeholder="请输入加分项直线经理评分"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="绩效月份" prop="performanceDate">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.performanceDate"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="请选择绩效月份">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="创建人ID" prop="creatorId">-->
<!--        <el-input-->
<!--          v-model="queryParams.creatorId"-->
<!--          placeholder="请输入创建人ID"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="创建人" prop="creatorName">-->
<!--        <el-input-->
<!--          v-model="queryParams.creatorName"-->
<!--          placeholder="请输入创建人"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="绩效小组打分人ID" prop="groupMemberId">-->
<!--        <el-input-->
<!--          v-model="queryParams.groupMemberId"-->
<!--          placeholder="请输入绩效小组打分人ID"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="绩效小组打分人" prop="groupMemberName">-->
<!--        <el-input-->
<!--          v-model="queryParams.groupMemberName"-->
<!--          placeholder="请输入绩效小组打分人"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="绩效小组打分时间" prop="groupMemberTime">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.groupMemberTime"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="请选择绩效小组打分时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="直线经理打分人ID" prop="managerId">-->
<!--        <el-input-->
<!--          v-model="queryParams.managerId"-->
<!--          placeholder="请输入直线经理打分人ID"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="直线经理小组打分人" prop="managerName">-->
<!--        <el-input-->
<!--          v-model="queryParams.managerName"-->
<!--          placeholder="请输入直线经理小组打分人"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="直线经理打分时间" prop="managerDatetime">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.managerDatetime"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="请选择直线经理打分时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:monthly:add']"
        >新增</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:monthly:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleBack"
          v-hasPermi="['system:monthly:back']"
        >撤回</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:monthly:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:monthly:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-add"
          size="mini"
          @click="handleAddKPI"
          v-hasPermi="['system:monthly:add']"
        >录入月度绩效</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="monthlyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="员工姓名" align="center" prop="employeeName" />
      <el-table-column label="岗位名称" align="center" prop="jobName" />
      <el-table-column label="绩效月份" align="center" prop="performanceDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.performanceDate, '{y}-{m}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="工单状态" align="center" prop="stateId">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.kpi_order_state_type" :value="scope.row.stateId"/>
        </template>
      </el-table-column>


      <el-table-column label="考核评分" align="center" prop="performanceScore" />
      <el-table-column label="月度工作绩效小组意见" align="center" prop="taskGroupOpinion" />
      <el-table-column label="月度工作直线经理意见" align="center" prop="taskManagerOpinion" />
      <el-table-column label="月度工作总评分" align="center" prop="totalScore" />
      <el-table-column label="直线经理加扣分意见" align="center" prop="taskManagerScore" />
      <el-table-column label="加分项绩效小组意见" align="center" prop="itemGroupOpinion" />
      <el-table-column label="加分项直线经理意见" align="center" prop="itemManagerOpinion" />
      <el-table-column label="加分项直线经理评分" align="center" prop="itemManagerScore" />
      <el-table-column label="创建人" align="center" prop="creatorName" />
      <el-table-column label="绩效小组打分人" align="center" prop="groupMemberName" />
      <el-table-column label="绩效小组打分时间" align="center" prop="groupMemberTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.groupMemberTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="直线经理小组打分人" align="center" prop="managerName" />
      <el-table-column label="直线经理打分时间" align="center" prop="managerDatetime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.managerDatetime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.stateId == 5"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:monthly:edit']"
          >修改</el-button>
          <el-button
            v-if="scope.row.stateId == 1"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleBack(scope.row)"
            v-hasPermi="['system:monthly:back']"
          >撤回</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:monthly:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改月度绩效管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="员工ID" prop="employeeId">
          <el-input v-model="form.employeeId" placeholder="请输入员工ID" />
        </el-form-item>
        <el-form-item label="员工姓名" prop="employeeName">
          <el-input v-model="form.employeeName" placeholder="请输入员工姓名" />
        </el-form-item>
        <el-form-item label="岗位" prop="jobId">
          <el-input v-model="form.jobId" placeholder="请输入岗位" />
        </el-form-item>
        <el-form-item label="岗位名称" prop="jobName">
          <el-input v-model="form.jobName" placeholder="请输入岗位名称" />
        </el-form-item>
        <el-form-item label="工单状态" prop="stateId">
          <el-input v-model="form.stateId" placeholder="请输入工单状态" />
        </el-form-item>
        <el-form-item label="工单状态" prop="stateName">
          <el-input v-model="form.stateName" placeholder="请输入工单状态" />
        </el-form-item>
        <el-form-item label="建设运营情况" prop="constructionOperationDetails">
          <el-input v-model="form.constructionOperationDetails" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="业务专项工作" prop="specialWork">
          <el-input v-model="form.specialWork" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="安全运维工作" prop="safetyOperation">
          <el-input v-model="form.safetyOperation" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="月度工作绩效小组意见" prop="taskGroupOpinion">
          <el-input v-model="form.taskGroupOpinion" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="总评分" prop="totalScore">
          <el-input v-model="form.totalScore" placeholder="请输入总评分" />
        </el-form-item>
        <el-form-item label="直线经理加扣分意见" prop="taskManagerScore">
          <el-input v-model="form.taskManagerScore" placeholder="请输入直线经理加扣分意见" />
        </el-form-item>
        <el-form-item label="月度工作直线经理意见" prop="taskManagerOpinion">
          <el-input v-model="form.taskManagerOpinion" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="加分项" prop="addingScoreItems">
          <el-input v-model="form.addingScoreItems" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="加分项绩效小组意见" prop="itemGroupOpinion">
          <el-input v-model="form.itemGroupOpinion" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="加分项直线经理评分" prop="itemManagerScore">
          <el-input v-model="form.itemManagerScore" placeholder="请输入加分项直线经理评分" />
        </el-form-item>
        <el-form-item label="加分项直线经理意见" prop="itemManagerOpinion">
          <el-input v-model="form.itemManagerOpinion" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="绩效月份" prop="performanceDate">
          <el-date-picker clearable
            v-model="form.performanceDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择绩效月份">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="创建人ID" prop="creatorId">
          <el-input v-model="form.creatorId" placeholder="请输入创建人ID" />
        </el-form-item>
        <el-form-item label="创建人" prop="creatorName">
          <el-input v-model="form.creatorName" placeholder="请输入创建人" />
        </el-form-item>
        <el-form-item label="绩效小组打分人ID" prop="groupMemberId">
          <el-input v-model="form.groupMemberId" placeholder="请输入绩效小组打分人ID" />
        </el-form-item>
        <el-form-item label="绩效小组打分人" prop="groupMemberName">
          <el-input v-model="form.groupMemberName" placeholder="请输入绩效小组打分人" />
        </el-form-item>
        <el-form-item label="绩效小组打分时间" prop="groupMemberTime">
          <el-date-picker clearable
            v-model="form.groupMemberTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择绩效小组打分时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="直线经理打分人ID" prop="managerId">
          <el-input v-model="form.managerId" placeholder="请输入直线经理打分人ID" />
        </el-form-item>
        <el-form-item label="直线经理小组打分人" prop="managerName">
          <el-input v-model="form.managerName" placeholder="请输入直线经理小组打分人" />
        </el-form-item>
        <el-form-item label="直线经理打分时间" prop="managerDatetime">
          <el-date-picker clearable
            v-model="form.managerDatetime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择直线经理打分时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMonthly, getMonthly, delMonthly, addMonthly, updateMonthly, backMonthly } from "@/api/system/monthly";
import { optionselect } from "@/api/system/post";

export default {
  name: "Monthly",
  dicts: ['kpi_job_type','kpi_order_state_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 月度绩效管理表格数据
      monthlyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //岗位选择框
      postOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        employeeId: null,
        employeeName: null,
        jobId: null,
        jobName: null,
        stateId: null,
        stateName: null,
        constructionOperationDetails: null,
        specialWork: null,
        safetyOperation: null,
        taskGroupOpinion: null,
        totalScore: null,
        taskManagerScore: null,
        taskManagerOpinion: null,
        addingScoreItems: null,
        itemGroupOpinion: null,
        itemManagerScore: null,
        itemManagerOpinion: null,
        performanceDate: null,
        creatorId: null,
        creatorName: null,
        groupMemberId: null,
        groupMemberName: null,
        groupMemberTime: null,
        managerId: null,
        managerName: null,
        managerDatetime: null,
        performanceScore: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        employeeId: [
          { required: true, message: "员工ID不能为空", trigger: "blur" }
        ],
        employeeName: [
          { required: true, message: "员工姓名不能为空", trigger: "blur" }
        ],
        jobId: [
          { required: true, message: "岗位不能为空", trigger: "blur" }
        ],
        stateId: [
          { required: true, message: "工单状态不能为空", trigger: "blur" }
        ],
        stateName: [
          { required: true, message: "工单状态不能为空", trigger: "blur" }
        ],
        constructionOperationDetails: [
          { required: true, message: "建设运营情况不能为空", trigger: "blur" }
        ],
        specialWork: [
          { required: true, message: "业务专项工作不能为空", trigger: "blur" }
        ],
        safetyOperation: [
          { required: true, message: "安全运维工作不能为空", trigger: "blur" }
        ],
        taskGroupOpinion: [
          { required: true, message: "月度工作绩效小组意见不能为空", trigger: "blur" }
        ],
        totalScore: [
          { required: true, message: "总评分不能为空", trigger: "blur" }
        ],
        taskManagerScore: [
          { required: true, message: "直线经理加扣分意见不能为空", trigger: "blur" }
        ],
        taskManagerOpinion: [
          { required: true, message: "月度工作直线经理意见不能为空", trigger: "blur" }
        ],
        itemGroupOpinion: [
          { required: true, message: "加分项绩效小组意见不能为空", trigger: "blur" }
        ],
        itemManagerScore: [
          { required: true, message: "加分项直线经理评分不能为空", trigger: "blur" }
        ],
        itemManagerOpinion: [
          { required: true, message: "加分项直线经理意见不能为空", trigger: "blur" }
        ],
        performanceDate: [
          { required: true, message: "绩效月份不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        creatorId: [
          { required: true, message: "创建人ID不能为空", trigger: "blur" }
        ],
        creatorName: [
          { required: true, message: "创建人不能为空", trigger: "blur" }
        ],
        groupMemberId: [
          { required: true, message: "绩效小组打分人ID不能为空", trigger: "blur" }
        ],
        groupMemberName: [
          { required: true, message: "绩效小组打分人不能为空", trigger: "blur" }
        ],
        managerId: [
          { required: true, message: "直线经理打分人ID不能为空", trigger: "blur" }
        ],
        managerName: [
          { required: true, message: "直线经理小组打分人不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.optionSelect();
  },
  methods: {
    /** 查询月度绩效管理列表 */
    getList() {
      this.loading = true;
      listMonthly(this.queryParams).then(response => {
        this.monthlyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    optionSelect(){
      optionselect().then(response => {
        this.postOptions = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: '',
        employeeId: null,
        employeeName: null,
        jobId: null,
        jobName: null,
        stateId: null,
        stateName: null,
        constructionOperationDetails: null,
        specialWork: null,
        safetyOperation: null,
        taskGroupOpinion: null,
        totalScore: null,
        taskManagerScore: null,
        taskManagerOpinion: null,
        addingScoreItems: null,
        itemGroupOpinion: null,
        itemManagerScore: null,
        itemManagerOpinion: null,
        performanceDate: null,
        createTime: null,
        creatorId: null,
        creatorName: null,
        groupMemberId: null,
        groupMemberName: null,
        groupMemberTime: null,
        managerId: null,
        managerName: null,
        managerDatetime: null,
        performanceScore: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加月度绩效管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      if (row.stateId != 5){
        this.$modal.msgSuccess("当前工单状态为【" + row.stateName + "】，不允许进行修改！");
      }

      let rowId = row.id || this.ids;
      if (this.ids.length > 1){
        this.$modal.msgSuccess("仅允许选择一条记录进行修改");
      }else{
        console.log("handleUpdate")
        this.$tab.openPage("编辑月度绩效", '/system/kpi-add/index',{op:'edit',id: rowId});
        this.getList();
      }

    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMonthly(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMonthly(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除月度绩效管理编号为"' + ids + '"的数据项？').then(function() {
        return delMonthly(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/monthly/export', {
        ...this.queryParams
      }, `monthly_${new Date().getTime()}.xlsx`)
    },
    /** 录入月度绩效按钮操作 */
    handleAddKPI() {
      console.log("handleAddKpi")
      this.$tab.openPage("录入月度绩效", '/system/kpi-add/index',{op:'add'});
    },
    /** 撤回按钮操作 */
    handleBack(row) {
      console.log("row----------------" + row)
      console.log("rowid----------" + row.id)

      let rowId = row.id || this.ids;
      if (this.ids.length > 1){
        this.$modal.msgSuccess("仅允许选择一条记录进行撤回");
      }else{
        backMonthly(rowId).then(response => {
          this.$modal.msgSuccess(response.msg);
          this.getList();
        });
        this.getList();
      }

    },
  }
};
</script>
